// pages/reset_shopping/reset_shopping.js
const db = wx.cloud.database();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    choose_flag: false,
    shop: null,
    array: [
      "书籍",
      "服饰",
      "鞋子",
      
      "电器",
      
      "手机",
      "零食",
      "数码"
    ],
    index: 0,
    src:"",
    piture_id:null,
    id:null
  },
  //改变选择
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let id = options.id;
    let src = options.src;
    let that = this;
    //根据商品id获得商品信息
    db.collection('users').doc(id).get().then(res => {
      console.log(res.data);
      that.setData({
        shop: res.data,
        src:res.data.src,
        id:id
      })
      that.setIndex();
    })
  },
  //获得种类的下标
  setIndex: function() {
    let len = this.data.array.length;
    for (let i = 0; i < len; i++) {
      if (this.data.array[i] == this.data.shop.species) {
        this.setData({
          index:i
        })
      }
    }
  },
  //选择图片
  choose: function () {
    let that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        //选择完成会先返回一个临时地址保存备用
        const tempFilePaths = res.tempFilePaths
        that.setData({
          src: tempFilePaths[0],
          choose_flag: true
        })
      }
    })
  },
  //确认更新数据
  formSubmit:function(e){
    let that=this;
    let piture_id = this.data.shop.src;
    let id=this.data.id;
    let time=new Date().getTime();
    let name = time + ".jpg";
    let index=this.data.index;
    if(that.data.choose_flag){
      //先把照片删除覆盖并且上传新的照片
      wx.cloud.deleteFile({
        fileList: [piture_id]
      }).then(res => {
        // 成功删除
        console.log("成功删除照片");
        console.log(res.fileList)
      }).catch(error => {
        // handle error
        console.log(error)
      });
    //上传照片；
      wx.cloud.uploadFile({
        cloudPath: name,
        filePath: that.data.src,
        success(res) {
          //上传成功后会返回永久地址
          that.setData({
            src: res.fileID
          })
          console.log(res.fileID)
          //更新数据
          db.collection('users').doc(id).update({
            // data 传入需要局部更新的数据
            data: {
              price: e.detail.value.price,
              name: e.detail.value.name,
              content: e.detail.value.content,
              src: res.fileID,
              species: that.data.array[index],
              time: time
            }
          }).then(result => {
            console.log("希望")
            console.log(result);
            wx.showToast({
              title: '修改成功',
            })
            wx.navigateBack({});
          })
        }
      })
    }else{
      db.collection('users').doc(id).update({
        // data 传入需要局部更新的数据
        data: {
          price: e.detail.value.price,
          name: e.detail.value.name,
          content: e.detail.value.content,
          species: that.data.array[index]
        }
      }).then(result => {
        console.log("希望")
        console.log(result);
        wx.showToast({
          title: '修改成功',
        })
        wx.navigateBack({});
      })
    }
  }
})